﻿using System;
using System.Collections.Generic;

namespace Poly2Tri
{
	// Token: 0x020003FE RID: 1022
	public class ConstrainedPointSet : PointSet
	{
		// Token: 0x06001C53 RID: 7251 RVA: 0x000B21F6 File Offset: 0x000B03F6
		public ConstrainedPointSet(List<TriangulationPoint> bounds) : base(bounds)
		{
			this.AddBoundaryConstraints();
		}

		// Token: 0x06001C54 RID: 7252 RVA: 0x000B221B File Offset: 0x000B041B
		public ConstrainedPointSet(List<TriangulationPoint> bounds, List<TriangulationConstraint> constraints) : base(bounds)
		{
			this.AddBoundaryConstraints();
			this.AddConstraints(constraints);
		}

		// Token: 0x06001C55 RID: 7253 RVA: 0x000B2248 File Offset: 0x000B0448
		public ConstrainedPointSet(List<TriangulationPoint> bounds, int[] indices) : base(bounds)
		{
			this.AddBoundaryConstraints();
			List<TriangulationConstraint> list = new List<TriangulationConstraint>();
			for (int i = 0; i < indices.Length; i += 2)
			{
				TriangulationConstraint item = new TriangulationConstraint(bounds[indices[i]], bounds[indices[i + 1]]);
				list.Add(item);
			}
			this.AddConstraints(list);
		}

		// Token: 0x17000403 RID: 1027
		// (get) Token: 0x06001C56 RID: 7254 RVA: 0x00003781 File Offset: 0x00001981
		public override TriangulationMode TriangulationMode
		{
			get
			{
				return TriangulationMode.Constrained;
			}
		}

		// Token: 0x06001C57 RID: 7255 RVA: 0x000B22BC File Offset: 0x000B04BC
		protected void AddBoundaryConstraints()
		{
			TriangulationPoint triangulationPoint = null;
			TriangulationPoint triangulationPoint2 = null;
			TriangulationPoint triangulationPoint3 = null;
			TriangulationPoint triangulationPoint4 = null;
			if (!base.TryGetPoint(base.MinX, base.MinY, out triangulationPoint))
			{
				triangulationPoint = new TriangulationPoint(base.MinX, base.MinY);
				this.Add(triangulationPoint);
			}
			if (!base.TryGetPoint(base.MaxX, base.MinY, out triangulationPoint2))
			{
				triangulationPoint2 = new TriangulationPoint(base.MaxX, base.MinY);
				this.Add(triangulationPoint2);
			}
			if (!base.TryGetPoint(base.MaxX, base.MaxY, out triangulationPoint3))
			{
				triangulationPoint3 = new TriangulationPoint(base.MaxX, base.MaxY);
				this.Add(triangulationPoint3);
			}
			if (!base.TryGetPoint(base.MinX, base.MaxY, out triangulationPoint4))
			{
				triangulationPoint4 = new TriangulationPoint(base.MinX, base.MaxY);
				this.Add(triangulationPoint4);
			}
			TriangulationConstraint tc = new TriangulationConstraint(triangulationPoint, triangulationPoint2);
			this.AddConstraint(tc);
			TriangulationConstraint tc2 = new TriangulationConstraint(triangulationPoint2, triangulationPoint3);
			this.AddConstraint(tc2);
			TriangulationConstraint tc3 = new TriangulationConstraint(triangulationPoint3, triangulationPoint4);
			this.AddConstraint(tc3);
			TriangulationConstraint tc4 = new TriangulationConstraint(triangulationPoint4, triangulationPoint);
			this.AddConstraint(tc4);
		}

		// Token: 0x06001C58 RID: 7256 RVA: 0x000B23E1 File Offset: 0x000B05E1
		public override void Add(Point2D p)
		{
			base.Add(p as TriangulationPoint, -1, true);
		}

		// Token: 0x06001C59 RID: 7257 RVA: 0x000B23F2 File Offset: 0x000B05F2
		public override void Add(TriangulationPoint p)
		{
			base.Add(p, -1, true);
		}

		// Token: 0x06001C5A RID: 7258 RVA: 0x000B2400 File Offset: 0x000B0600
		public override bool AddRange(List<TriangulationPoint> points)
		{
			bool flag = true;
			foreach (TriangulationPoint p in points)
			{
				flag = (base.Add(p, -1, true) && flag);
			}
			return flag;
		}

		// Token: 0x06001C5B RID: 7259 RVA: 0x000B2468 File Offset: 0x000B0668
		public bool AddHole(List<TriangulationPoint> points, string name)
		{
			if (points == null)
			{
				return false;
			}
			List<Contour> list = new List<Contour>();
			int i = 0;
			Contour item = new Contour(this, points, Point2DList.WindingOrderType.Unknown);
			list.Add(item);
			if (this.mPoints.Count > 1)
			{
				int count = list[i].Count;
				for (int j = 0; j < count; j++)
				{
					base.ConstrainPointToBounds(list[i][j]);
				}
			}
			while (i < list.Count)
			{
				list[i].RemoveDuplicateNeighborPoints();
				list[i].WindingOrder = Point2DList.WindingOrderType.CCW;
				bool flag = true;
				Point2DList.PolygonError polygonError = list[i].CheckPolygon();
				while (flag && polygonError != Point2DList.PolygonError.None)
				{
					if ((polygonError & Point2DList.PolygonError.NotEnoughVertices) == Point2DList.PolygonError.NotEnoughVertices)
					{
						flag = false;
					}
					else if ((polygonError & Point2DList.PolygonError.NotSimple) == Point2DList.PolygonError.NotSimple)
					{
						List<Point2DList> list2 = PolygonUtil.SplitComplexPolygon(list[i], list[i].Epsilon);
						list.RemoveAt(i);
						foreach (Point2DList l in list2)
						{
							Contour contour = new Contour(this);
							contour.AddRange(l);
							list.Add(contour);
						}
						polygonError = list[i].CheckPolygon();
					}
					else if ((polygonError & Point2DList.PolygonError.Degenerate) == Point2DList.PolygonError.Degenerate)
					{
						list[i].Simplify(base.Epsilon);
						polygonError = list[i].CheckPolygon();
					}
					else if ((polygonError & Point2DList.PolygonError.AreaTooSmall) == Point2DList.PolygonError.AreaTooSmall || (polygonError & Point2DList.PolygonError.SidesTooCloseToParallel) == Point2DList.PolygonError.SidesTooCloseToParallel || (polygonError & Point2DList.PolygonError.TooThin) == Point2DList.PolygonError.TooThin || (polygonError & Point2DList.PolygonError.Unknown) == Point2DList.PolygonError.Unknown)
					{
						flag = false;
					}
				}
				if (!flag && list[i].Count != 2)
				{
					list.RemoveAt(i);
				}
				else
				{
					i++;
				}
			}
			bool result = true;
			i = 0;
			while (i < list.Count)
			{
				int count2 = list[i].Count;
				if (count2 < 2)
				{
					i++;
					result = false;
				}
				else
				{
					if (count2 == 2)
					{
						uint key = TriangulationConstraint.CalculateContraintCode(list[i][0], list[i][1]);
						TriangulationConstraint tc = null;
						if (!this.mConstraintMap.TryGetValue(key, out tc))
						{
							tc = new TriangulationConstraint(list[i][0], list[i][1]);
							this.AddConstraint(tc);
						}
					}
					else
					{
						Contour contour2 = new Contour(this, list[i], Point2DList.WindingOrderType.Unknown);
						contour2.WindingOrder = Point2DList.WindingOrderType.CCW;
						contour2.Name = name + ":" + i.ToString();
						this.mHoles.Add(contour2);
					}
					i++;
				}
			}
			return result;
		}

		// Token: 0x06001C5C RID: 7260 RVA: 0x000B2764 File Offset: 0x000B0964
		public bool AddConstraints(List<TriangulationConstraint> constraints)
		{
			if (constraints == null || constraints.Count < 1)
			{
				return false;
			}
			bool flag = true;
			foreach (TriangulationConstraint triangulationConstraint in constraints)
			{
				if (base.ConstrainPointToBounds(triangulationConstraint.P) || base.ConstrainPointToBounds(triangulationConstraint.Q))
				{
					triangulationConstraint.CalculateContraintCode();
				}
				TriangulationConstraint tc = null;
				if (!this.mConstraintMap.TryGetValue(triangulationConstraint.ConstraintCode, out tc))
				{
					tc = triangulationConstraint;
					flag = (this.AddConstraint(tc) && flag);
				}
			}
			return flag;
		}

		// Token: 0x06001C5D RID: 7261 RVA: 0x000B2820 File Offset: 0x000B0A20
		public bool AddConstraint(TriangulationConstraint tc)
		{
			if (tc == null || tc.P == null || tc.Q == null)
			{
				return false;
			}
			if (this.mConstraintMap.ContainsKey(tc.ConstraintCode))
			{
				return true;
			}
			TriangulationPoint triangulationPoint;
			if (base.TryGetPoint(tc.P.X, tc.P.Y, out triangulationPoint))
			{
				tc.P = triangulationPoint;
			}
			else
			{
				this.Add(tc.P);
			}
			if (base.TryGetPoint(tc.Q.X, tc.Q.Y, out triangulationPoint))
			{
				tc.Q = triangulationPoint;
			}
			else
			{
				this.Add(tc.Q);
			}
			this.mConstraintMap.Add(tc.ConstraintCode, tc);
			return true;
		}

		// Token: 0x06001C5E RID: 7262 RVA: 0x000B28EC File Offset: 0x000B0AEC
		public bool TryGetConstraint(uint constraintCode, out TriangulationConstraint tc)
		{
			return this.mConstraintMap.TryGetValue(constraintCode, out tc);
		}

		// Token: 0x06001C5F RID: 7263 RVA: 0x000B28FB File Offset: 0x000B0AFB
		public int GetNumConstraints()
		{
			return this.mConstraintMap.Count;
		}

		// Token: 0x06001C60 RID: 7264 RVA: 0x000B2908 File Offset: 0x000B0B08
		public Dictionary<uint, TriangulationConstraint>.Enumerator GetConstraintEnumerator()
		{
			return this.mConstraintMap.GetEnumerator();
		}

		// Token: 0x06001C61 RID: 7265 RVA: 0x000B2918 File Offset: 0x000B0B18
		public int GetNumHoles()
		{
			int num = 0;
			foreach (Contour contour in this.mHoles)
			{
				num += contour.GetNumHoles(false);
			}
			return num;
		}

		// Token: 0x06001C62 RID: 7266 RVA: 0x000B297C File Offset: 0x000B0B7C
		public Contour GetHole(int idx)
		{
			if (idx < 0 || idx >= this.mHoles.Count)
			{
				return null;
			}
			return this.mHoles[idx];
		}

		// Token: 0x06001C63 RID: 7267 RVA: 0x000B29A4 File Offset: 0x000B0BA4
		public int GetActualHoles(out List<Contour> holes)
		{
			holes = new List<Contour>();
			foreach (Contour contour in this.mHoles)
			{
				contour.GetActualHoles(false, ref holes);
			}
			return holes.Count;
		}

		// Token: 0x06001C64 RID: 7268 RVA: 0x000B2A10 File Offset: 0x000B0C10
		protected void InitializeHoles()
		{
			Contour.InitializeHoles(this.mHoles, this, this);
			foreach (Contour contour in this.mHoles)
			{
				contour.InitializeHoles(this);
			}
		}

		// Token: 0x06001C65 RID: 7269 RVA: 0x000B2A7C File Offset: 0x000B0C7C
		public override bool Initialize()
		{
			this.InitializeHoles();
			return base.Initialize();
		}

		// Token: 0x06001C66 RID: 7270 RVA: 0x000B2A8C File Offset: 0x000B0C8C
		public override void Prepare(TriangulationContext tcx)
		{
			if (!this.Initialize())
			{
				return;
			}
			base.Prepare(tcx);
			foreach (KeyValuePair<uint, TriangulationConstraint> keyValuePair in this.mConstraintMap)
			{
				TriangulationConstraint value = keyValuePair.Value;
				tcx.NewConstraint(value.P, value.Q);
			}
		}

		// Token: 0x06001C67 RID: 7271 RVA: 0x000B2AEC File Offset: 0x000B0CEC
		public override void AddTriangle(DelaunayTriangle t)
		{
			base.Triangles.Add(t);
		}

		// Token: 0x04001D8E RID: 7566
		protected Dictionary<uint, TriangulationConstraint> mConstraintMap = new Dictionary<uint, TriangulationConstraint>();

		// Token: 0x04001D8F RID: 7567
		protected List<Contour> mHoles = new List<Contour>();
	}
}
